/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package byblos.negocio.acomodacao;

import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.io.Serializable;
import java.util.ArrayList;
import javax.persistence.CascadeType;

/**
 *
 * @author helena
 */
@Entity
public class TabelaTarifa implements Serializable {

    @Id
    @GeneratedValue
    private int id;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "tabela")
    private List<ItemTabelaTarifa> itens = new ArrayList<ItemTabelaTarifa>(TipoAcomodacao.values().length);

    public double getValor(TipoAcomodacao tipo) {
        return getItem(tipo).getValor();
    }

    public void setValor(double valor, TipoAcomodacao tipo) {
        getItem(tipo).setValor(valor);
    }

    private ItemTabelaTarifa getItem(TipoAcomodacao tipo) {
        for (ItemTabelaTarifa item : itens) {
            if (item.getTipo() == tipo) {
                return item;
            }
        }
        ItemTabelaTarifa item = new ItemTabelaTarifa();
        item.setTabela(this);
        item.setTipo(tipo);
        itens.add(item);
        return item;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<ItemTabelaTarifa> getItens() {
        return itens;
    }

    public void setItens(List<ItemTabelaTarifa> itens) {
        this.itens = itens;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final TabelaTarifa other = (TabelaTarifa) obj;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 43 * hash + this.id;
        return hash;
    }
}
